<?php
namespace Controller;

require_once("Controller/mainPageController.php");
require_once("Controller/DALcontroller.php");
require_once("View/quizView.php");
require_once("View/mainPageView.php");
require_once("Model/quizModel.php");
require_once("Model/verificationModel.php");

class applicationController{
	/**
	 * @var $body
	 * @var $view
	 * @var $controller
	 * @var $model
	 * @var $creator
	 * @var $mysqli
	*/
	private $body;
	private $view;
	private $controller;
	private $DALcontroller;
	private $model;
	private $creator;
	private $mysqli;
	
	/**
	 * @var $quizlist
	 */
	private $quizlist = array();
	
	public function __construct(){
		$this->mysqli = new \mysqli("quiz-166006.mysql.binero.se", "166006_bz22041", "Lillstrumpa1", "166006-quiz");
		$this->model = new \Model\verificationModel();
		$this->controller = new \Controller\mainPageController($this->model);
		$this->DALcontroller = new \Controller\DALcontroller($this->mysqli);
		$this->view = new \View\mainPageView();
		
		//$this->quizlist[] = $this->DALcontroller->getAllQuizzes();
		
	}
	/**
	 * @return Page (HTML)
	 */
	public function runMainPage(){
		if($this->controller->loginAttempt()){
			$this->loginAttempt();
		}
		else if($this->controller->createNewQuizAttempt()){
			$this->body = $this->controller->getCreateQuizPage();
		}		
		/**
		 * @TODO: FUNCTIONALITY FOR SAVING THE CREATED QUIZ, USING QUIZMODEL (IN PROGRESS...)
		 * @TODO: ADD FUNCTIONALITY FOR SAVING ALL THE SELECTED ANSWERS IN QUIZMODEL ARRAY WHEN FINISHING THE QUIZ!
		 * @TODO: CHECK IF THE METHODS FOR GETTING INPUT INFO SHOULD BE PRIVATE!
		 */
		else if($this->controller->createNewQuizStageTwo()){
			$this->createNewQuizStageTwo();
		}
		else if($this->controller->finishQuizCreation()){
			$this->finishQuizCreation();
		}
		else{
			$this->body = $this->controller->getLoginPage();	
		}	
		return $this->view->getMainPageHTML($this->body);
	}
	
	//IF LOGIN IS ATTEMPTED
	public function loginAttempt(){
		if($this->controller->userIsLoggedIn()){
			$this->body = $this->controller->getQuizList($this->quizlist);
		}
		else if($this->controller->adminIsLoggedIn()){
			$this->body = $this->controller->getAdminPage();
		}
		else{
			$this->body = $this->controller->getLoginPage();
		}
	}
	
	//IF ADMIN TRIES TO PROCEED TO NEXT PART IN QUIZ CREATION
	public function createNewQuizStageTwo(){
		//The quiz name and number of questions is pulled from input from previous page.
		$quizName = $this->controller->getQuizName();
		$numOfQs = $this->controller->getNumberOfQuestions();	
		
		try{	
			if($this->controller->verifyQuizInput($quizName, $numOfQs)){					
				//The first part of the page is created.
				$this->body = $this->controller->getCreateQuizPageTwo($quizName);
				
				//The questions are placed into the page and the form is closed afterwards.
				for($i = 1; $i <= $numOfQs; $i++){
					$this->body .= $this->controller->generateQuestions($i);
				}			
				$this->body .= $this->controller->closeForm();
			}
		}
		catch(\Exception $e){
			$this->controller->createQuizStageOneFailed();
			$this->body = $this->controller->getCreateQuizPage();
		}
	}
	
	//IF ADMIN TRIES TO FINISH QUIZ CREATION
	public function finishQuizCreation(){
		$quizName = $this->controller->getQuizName();	
		$numOfQs = $this->controller->getNumberOfQuestions();
		$questions = array();
		$answers = array();
		$answerOptions = array();
		
		try{
			//TODO: Improve selection of the answeroptions
			for($i = 1; $i <= $numOfQs; $i++){
				$questions[] = $this->controller->getSelectedQuestion($i);
				$answers[] = $this->controller->getSelectedAnswer($i);
				$answerOptions[] = $this->controller->getSelectedAnswerOption1();
				$answerOptions[] = $this->controller->getSelectedAnswerOption2();
				$answerOptions[] = $this->controller->getSelectedAnswerOption3();
			}
			
			$this->controller->validateArray($questions);
			$this->controller->validateArray($answers);
			$this->controller->validateArray($answerOptions);
		}
		catch(\Exception $e){
			$this->controller->createQuizStageTwoFailed();
			$this->body->getCreateQuizPageTwo();
		}
		
		//A new quiz object is created for this specific quiz.
		$this->creator = new \Model\quizModel($quizName, $questions, $answers, $answerOptions);
		$this->DALcontroller->addQuizToDatabase($this->creator);
	}
}
